package com.qpf.mycms.website.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qpf.mycms.api.LoginApi;
import com.qpf.mycms.api.exception.SystemException;
import com.qpf.mycms.api.exception.enums.BusinessExceptionEnum;
import com.qpf.mycms.api.exception.enums.ClientExceptionEnum;
import com.qpf.mycms.api.factory.PageFactory;
import com.qpf.mycms.api.factory.PageResultFactory;
import com.qpf.mycms.api.pojo.PageResult;
import com.qpf.mycms.api.pojo.UserRequest;
import com.qpf.mycms.api.pojo.dto.UserDto;
import com.qpf.mycms.website.member.domain.MemberOpenid;
import com.qpf.mycms.website.member.domain.MemberUser;
import com.qpf.mycms.website.member.service.MemberUserService;
import com.qpf.mycms.website.member.mapper.MemberUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
* @author admin
* @description 针对表【member_user(会员用户)】的数据库操作Service实现
* &#064;createDate  2024-08-07 10:40:10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MemberUserServiceImpl extends ServiceImpl<MemberUserMapper, MemberUser>
    implements MemberUserService, LoginApi {

    private static final int ACCOUNT_MIN_LENGTH = 3;
    private static final int ACCOUNT_MAX_LENGTH = 16;

    @Override
    public void add(UserRequest request) {
        MemberUser user = new MemberUser();
        checkUsername(request.getUsername());
//        checkPassword(request.getPassword());
        BeanUtils.copyProperties(request, user);
        save(user);
    }

    private void checkUsername(String username) {
        if (ObjectUtils.isEmpty((username))) {
            throw new SystemException(ClientExceptionEnum.PARAM_NOT_NULL, "账号");
        }
        if (username.length() < ACCOUNT_MIN_LENGTH || username.length() > ACCOUNT_MAX_LENGTH) {
            throw new SystemException(ClientExceptionEnum.PARAM_LENGTH, "账号", ACCOUNT_MIN_LENGTH, ACCOUNT_MAX_LENGTH);
        }
        UserRequest request = new UserRequest();
        request.setUsername(username);
        if (queryCount(request) > 0) {
            throw new SystemException(ClientExceptionEnum.VALUE_EXISTED, "账号");
        }
    }

    private void checkPassword(String password) {
        if (ObjectUtils.isEmpty((password))) {
            throw new SystemException(ClientExceptionEnum.PARAM_NOT_NULL, "密码");
        }
    }
    @Override
    public void del(UserRequest request) {
        MemberUser user = findUser(request);
        removeById(user.getId());
    }

    @Override
    public void edit(UserRequest request) {
        MemberUser user = findUser(request);
        Long id = user.getId();
        user = new MemberUser();
        user.setId(id);
        BeanUtils.copyProperties(request, user);
        updateById(user);
    }

    @Override
    public MemberUser detail(UserRequest request) {
        return findUser(request);
    }

    @Override
    public PageResult<MemberUser> findPage(UserRequest request) {
        Page<MemberUser> page = page(PageFactory.defaultPage(), createWrapper(request));
        return PageResultFactory.createPageResult(page);
    }

    private LambdaQueryWrapper<MemberUser> createWrapper(UserRequest request) {
        LambdaQueryWrapper<MemberUser> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isEmpty(request)) {
            return wrapper;
        }
        wrapper.eq(!ObjectUtils.isEmpty(request.getId()), MemberUser::getId, request.getId());
        wrapper.eq(!ObjectUtils.isEmpty(request.getUsername()), MemberUser::getUsername, request.getUsername());
        wrapper.eq(!ObjectUtils.isEmpty(request.getMobile()), MemberUser::getMobile, request.getMobile());
        wrapper.eq(!ObjectUtils.isEmpty(request.getEmail()), MemberUser::getEmail, request.getEmail());
        return wrapper;
    }

    private MemberUser findUser(UserRequest request) {
        if (ObjectUtils.isEmpty((request.getId()))) {
            MemberUser user = this.getOne(createWrapper(request));
            if (ObjectUtils.isEmpty(user)) {
                throw new SystemException(ClientExceptionEnum.USER_NOT_EXITS);
            }
            return user;
        }

        MemberUser user = getById(request.getId());
        if (ObjectUtils.isEmpty(user)) {
            throw new SystemException(ClientExceptionEnum.USER_NOT_EXITS);
        }
        return user;
    }

    public int queryCount(UserRequest request) {
        return this.count(createWrapper(request));
    }

    // ------------------------- 通用方法 -------------------------

    /**
     * 验证账号和密码的方法
     * @param username 账号
     * @param password 密码
     * @return UserDto
     */
    @Override
    public UserDto login(String username, String password) {
        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)) {
            throw new SystemException(ClientExceptionEnum.PARAM_INCORRECT, "账号或密码");
        }
        UserRequest request = new UserRequest();
        request.setUsername(username);
        MemberUser user = detail(request);
        if (ObjectUtils.isEmpty(user)||ObjectUtils.isEmpty(password) || !password.equals(user.getPassword())) {
            throw new SystemException(ClientExceptionEnum.PARAM_INCORRECT, "账号或密码");
        }
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        return userDto;
    }

    @Override
    public UserDto register(UserRequest request) {
        if (ObjectUtils.isEmpty(request.getUsername())
                && ObjectUtils.isEmpty(request.getEmail())
                && ObjectUtils.isEmpty(request.getMobile())) {
            throw new SystemException(ClientExceptionEnum.PARAM_NOT_NULL, "账户");
        }
        add(request);
        MemberUser user = getOne(createWrapper(request));
        if (ObjectUtils.isEmpty(user)) {
            throw new SystemException(BusinessExceptionEnum.REGISTER_FAILED);
        }
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        return userDto;
    }
}




